package main

import (
	"database/sql"
	"fmt"
	_ "github.com/bmizerany/pq"
)

func main() {
	db, err := sql.Open("postgres", "user=postgres password=root dbname=xm sslmode=disable")

	showErr(err)
	stmt, err := db.Prepare("INSERT INTO test(id,name) values ($1,$2)")
	showErr(err)

	// stmt.Exec(2, "user")
	res, err := stmt.Exec(4, "user")
	showErr(err)

	affect, err := res.RowsAffected()
	fmt.Println(affect)

	rows, err := db.Query("select * from test")

	for rows.Next() {
		var id int
		var name string
		rows.Scan(&id, &name)
		fmt.Print(id)
		fmt.Println("-" + name)
	}

	// id, err := res.LastInsertId()
	// showErr(err)

	// fmt.Println(id)

	db.Close()

}

func showErr(err error) {
	if err != nil {
		fmt.Println(err.Error())
	}
}
